package com.qing.library.utils;

import android.content.Context;
import android.os.Build;
import android.os.Environment;

import java.io.File;

/**
 * Created by DaiQing on 2016/7/25 0025.
 */
public class FileUtils {

    /**
     * 检查是否存在SD卡
     * @return
     */
    public static boolean hasSDCard() {
        return Environment.MEDIA_MOUNTED.equals(Environment
                .getExternalStorageState());
    }

    /**
     * 检查sd卡是否存在，不存在则创建
     * @param filePath
     * @return
     */
    public static boolean checkDir(String filePath){

        File file = new File(filePath);
        if (!file.exists()) return file.mkdirs();
        return true;
    }

    /**
     * 获取当前应用SD卡缓存目录
     * @param context
     * @return
     */
    public static String getSDCacheDir(Context context){

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {  //api大于8的版本

            return context.getExternalCacheDir().getPath();  //目录为/mnt/sdcard/Android/data/com.mt.mtpp/cache
        }
        String cacheDir = "/Android/data/" + context.getPackageName() + "/cache";
        return Environment.getExternalStorageDirectory().getPath() + cacheDir;
    }
}
